﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Regions;
using EQual.Infrastructure;

namespace EQual.MainModule.QualityEvents
{
    /// <summary>
    ///Provides functionality for all Quality Events Controllers
    /// </summary>
    public abstract class QualityEventController : EQual.Infrastructure.MVPFramework.Presenters.ControllerBase<QualityEventGridView , DataAccesLayer.QualityEvent>, EQual.Infrastructure.IModuleController 
    {
        protected DataAccesLayer.ProjectsDataAccesLayer ProjectAccesLayer;

        protected QualityEventProjectPresenter  projectsPresenter;

        protected abstract string EventType { get; }

        protected DataAccesLayer.QualityEventType SelectedQualityEventType
        {
            get { return ViewModel.QualityEventType ; }
            set { ViewModel.QualityEventType = value; InjectViews(); }
        }

         protected  IRegionManager _regionManager;

        public QualityEventController(IRegionManager regionManager)
            : this()
        {
            this._regionManager = regionManager;
        }

        QualityEventDetailsView detailsView;

        public QualityEventController()
        {
            this.View = new QualityEventGridView();
            detailsView = new QualityEventDetailsView();

            projectsPresenter = new QualityEventProjectPresenter();

            this.View.ItemAdded += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemAdded);
            this.View.ItemSelected += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemSelected);

            this.ProjectAccesLayer = new DataAccesLayer.ProjectsDataAccesLayer();
        }

        EQual.MainModule.DetailsContainer containerView = new DetailsContainer();


        protected virtual void InjectViews()
        {

            //throw in to the datails region the datails view

            IRegion region = _regionManager.Regions[RegionNames.ActionRegion];

            if (!region.Views.Contains(containerView))
            {

                containerView.AddView(GetDecoratedView(projectsPresenter.View   , "Projects"));
                containerView.AddView(GetDecoratedView(detailsView, ""));

                region.Add(containerView);
            }

            //activate the current view
            region.Activate(containerView);
        }

        public object GetDecoratedView(object view, string header)
        {
            return new DataGridDecorator(view, header);
        }

        public override void Dispaly(DataAccesLayer.QualityEvent item)
        {
            InjectViews();
            projectsPresenter.ActiveItem = item;
            detailsView.ActiveItem = item;
        }

        protected override void InitializeViews()
        {
            base.InitializeViews();
        }

        protected virtual void View_ItemSelected(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            this.ActiveItem = e.Value as DataAccesLayer.QualityEvent;
        }

        protected virtual void View_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
        {
            this.ProjectAccesLayer.AddNewQualityEventWithType(e.Value,SelectedQualityEventType);
        }

        #region IModuleController Members

        protected QualityEventPresentationModel ViewModel;

        public void Run()
        {
            ViewModel = GetNewPresentationModel();

            View.Model = ViewModel;

            SetUpViews();

            this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion, () => this.View);
            this._regionManager.Regions[RegionNames.MainRegion].Activate(this.View);
        }

        protected virtual QualityEventPresentationModel GetNewPresentationModel()
        {
          return new QualityEventPresentationModel();
        }

        protected virtual void SetUpViews()
        {
            SelectedQualityEventType = ProjectAccesLayer.GetQualityEventsType(EventType);
        }

        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    public class QualityComplaintsController : QualityEventController
    {
        protected static readonly string eventType = "Παράπονο";


        protected override string EventType { get { return eventType; } }
   
        public QualityComplaintsController(IRegionManager regionManager)
            : base()
        {
            this._regionManager = regionManager;
        }

        protected override void SetUpViews()
        {
            base.SetUpViews();
        }
    }

    public class QualityNonConformitiesController : QualityEventController
    {
        protected static readonly string eventType = "Μη-συμμώρφωση";

        protected override string EventType { get { return eventType; } }

        public QualityNonConformitiesController(IRegionManager regionManager)
            : base()
        {
            this._regionManager = regionManager;
        }

        protected override void SetUpViews()
        {
            base.SetUpViews();
        }
    }

    public class QualityInappropriatesController : QualityEventController
    {
        protected static readonly string eventType = "Ακαταλληλο";

        protected override string EventType { get { return eventType; } }

        public QualityInappropriatesController(IRegionManager regionManager)
            : base()
        {
            this._regionManager = regionManager;
        }

        protected override void SetUpViews()
        {
            base.SetUpViews();
        }
    }
}
